/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import model.HinhThucHopDong;
import model.HoSo;
import model.HopDongLaoDong;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class HopDongDao {
    private static Session session;
    
    public HopDongDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertHopDong
     */
    public boolean insertHopDong(String maHopDong, String maNV, int hinhThuc, Date ngayKyHopDong, Date ngayKetThucHopDong, String soLanKyHopDong, String file){
        try {
            session.beginTransaction();
            
            HoSo hoSo = (HoSo)session.get(HoSo.class, maNV);
            HinhThucHopDong hinhThucHopDong = (HinhThucHopDong)session.get(HinhThucHopDong.class, hinhThuc);
            HopDongLaoDong hopDong = new HopDongLaoDong(maHopDong, hoSo, hinhThucHopDong, ngayKyHopDong, ngayKetThucHopDong, soLanKyHopDong, file);
            
            hoSo.getHopDongLaoDongs().add(hopDong);
            hinhThucHopDong.getHopDongLaoDongs().add(hopDong);
            
            session.save(hoSo);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updateHopDong
     */
    public boolean updateHopDong(String maHopDong, String maNV, int hinhThuc, Date ngayKyHopDong, Date ngayKetThucHopDong, String soLanKyHopDong, String file){
        try {
            session.beginTransaction();
            
            HoSo hoSo = (HoSo)session.get(HoSo.class, maNV);
            HinhThucHopDong hinhThucHopDong = (HinhThucHopDong)session.get(HinhThucHopDong.class, hinhThuc);
            HopDongLaoDong hopDong = (HopDongLaoDong)session.get(HopDongLaoDong.class, maHopDong);
            hopDong.setFile(file);
            hopDong.setNgayKyHopDong(ngayKyHopDong);
            hopDong.setNgayKetThucHopDong(ngayKetThucHopDong);
            hopDong.setSoLanKyHopDong(soLanKyHopDong);
            
            hopDong.setHinhThucHopDong(hinhThucHopDong);
            hopDong.setHoSo(hoSo);
            
            hinhThucHopDong.getHopDongLaoDongs().add(hopDong);
            hoSo.getHopDongLaoDongs().add(hopDong);
            
            session.update(hoSo);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deleteHopDong
     */
    public boolean deleteHopDong(String maHD){
        try {
            session.beginTransaction();
            
            HopDongLaoDong hopDong = (HopDongLaoDong)session.get(HopDongLaoDong.class, maHD);
            
            session.delete(hopDong);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
}
